<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="志愿活动名称" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入活动名称"
              clearable
              size="small"
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">

          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>
          <el-table :data="volunteerList" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"/>
            <el-table-column prop="volunteerName" label="活动名称" align="center"/>
            <el-table-column  v-if="false" prop="id" label="id" align="center" hidden/>
            <el-table-column prop="volunteerContent" label="活动内容" align="center">
            </el-table-column>
            <el-table-column prop="volunteerArea" label="活动地点" align="center"/>
            <el-table-column prop="point" label="参与可获得积分值" align="center"/>
            <el-table-column prop="number" label="计划人数" align="center"/>
            <el-table-column prop="participateNumber" label="参与人数" align="center"/>
            <el-table-column prop="startTime" label="活动时间" align="center"/>
            <el-table-column prop="endTime" label="报名截止时间" align="center"/>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              class-name="small-padding fixed-width"
            >
              <template v-if="scope.row.id !== 1" slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  plain
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />

  <!-- 添加或修改参数配置对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="活动名称" prop="volunteerName">
            <el-input
              v-model="form.volunteerName"
              placeholder="请输入活动名称"
            />
          </el-form-item>

          <el-form-item label="活动内容" prop="volunteerContent">
            <el-input
              v-model="form.volunteerContent"
              placeholder="请输入活动内容"
            />
          </el-form-item>

          <el-form-item label="活动地点" prop="volunteerArea">
            <el-input
              v-model="form.volunteerArea"
              placeholder="请输入活动地点"
            />
          </el-form-item>

          <el-form-item label="参与获得积分值" prop="point">
            <el-input
              v-model="form.point"
              placeholder="请输入积分值"
              type="number"
            />
          </el-form-item>

          <el-form-item label="可报名人数" prop="number">
            <el-input
              v-model="form.number"
              placeholder="请输入报名人数"
              type="number"
            />
          </el-form-item>

          <el-form-item label="活动开始时间" prop="startTime">
            <el-date-picker
              v-model="form.startTime"
              type="datetime"
              placeholder="请选择日期时间"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="报名截止时间" prop="endTime">
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请选择日期时间"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import {addVolunteer, allVolunteerList, deleteVolunteer, updateVolunteer} from "@/api/volunteer/manage";
export default {
  data() {
    var checkPoint = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('参与积分值不能为空'));
      }
      setTimeout(() => {

          if (value < 0) {
            callback(new Error('积分值必须大于0'));
          } else {
            callback();
          }

      }, 1000);
    };
    var checkNumber = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('参与人数不能为空'));
      }
      setTimeout(() => {

          if (value < 0) {
            callback(new Error('参与人数必须大于0'));
          } else {
            callback();
          }

      }, 1000);
    };
    return {
      showSearch: true,
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        volunteerName: undefined,
      },
      volunteerList:null,
      total:null,
      title: '',
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 表单参数
      form: {},
      rules: {
        volunteerName: [
          {required: true, trigger: 'blur' ,message: '活动名称不能为空' }
        ],
        volunteerContent: [
          {required: true, message: '活动内容不能为空', trigger: 'blur'},
        ],
        volunteerArea: [
          {required: true, message: '活动地点不能为空', trigger: 'blur'},
        ],
        point: [
          {required: true, validator: checkPoint, trigger: 'blur'},
        ],
        number: [
          {required: true, validator: checkNumber, trigger: 'blur'},
        ],
        startTime:[
          {required: true, message: '活动时间不能为空', trigger: 'blur'},
        ],
        endTime:[
          {required: true, message: '报名截止时间不能为空', trigger: 'blur'},
        ],
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      }
    }
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否删除当前选中数据项？')
        .then(function () {
          return deleteVolunteer(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    reset() {
      this.form = {
        volunteerName: undefined,
        volunteerContent: undefined,
        point : undefined,
        number: undefined,
        participateNumber: undefined,
        createTime: undefined,
        id:undefined
      }
      this.resetForm('form')
    },
    handleUpdate(row) {
      this.reset()
      const ids = row.id || this.ids
      this.form = row
      this.open = true
      this.title = '修改活动';
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 添加积分 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加志愿活动'

    },
    getList() {
      this.loading = true
      allVolunteerList(this.queryParams).then((response) => {
        this.volunteerList = response.list
        this.total = response.total
        this.loading = false
      });
    },
    handleStatusChange(row) {
      const text = row.receiveFlag === '0' ? '领取' : '修改';
      this.$modal
        .confirm('确认将该条记录改为'+text+'状态')
        .then(function() {
          return changeExchangeStatus(row.id, row.receiveFlag === '0'?'1':'0').then((response) => {
            row.receiveFlag = row.receiveFlag === '0'?'1':'0'
          })
        })
        .then(() => {
          this.$modal.msgSuccess(text + '成功');
          this.getList();
        })
        .catch(function() {
          row.status = row.status === '0' ? '1' : '0'
        })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateVolunteer(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }else{
            addVolunteer(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
  },
  created() {
    this.getList()
  },
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>


